package com.eqdd.yiqidian.base;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.ShowCommonTabActivityCustom;
import com.eqdd.yiqidian.adapter.MyFragmentPagerAdapter;
import com.eqdd.yiqidian.bean.TabEntity;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 吕志豪 .
 * @date 17-11-23  上午9:03.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public abstract class BaseShowActivity extends BaseActivity {
    protected ShowCommonTabActivityCustom dataBinding;
    protected List<String> titles = new ArrayList<>();
    private ArrayList<Fragment> fragments;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_show_commen_tab);
        initTitle();
    }

    protected abstract void initTitle();

    @Override
    public void setView() {
        fragments = new ArrayList<>();
        ArrayList<CustomTabEntity> customTabEntities = new ArrayList<>();
        for (int i = 0; i < titles.size(); i++) {
            TabEntity tabEntity = new TabEntity(titles.get(i), 0, 0);
            customTabEntities.add(tabEntity);
            fragments.add(getFragment(i));
        }

        dataBinding.viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), titles, fragments));
        dataBinding.commonTabLayout.setTabData(customTabEntities);
        dataBinding.commonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                dataBinding.viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        dataBinding.viewPager.addOnPageChangeListener(
                new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                    }

                    @Override
                    public void onPageSelected(int position) {
                        dataBinding.commonTabLayout.setCurrentTab(position);
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {

                    }
                }
        );
    }

    protected abstract Fragment getFragment(int i);

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        fragments.get(dataBinding.viewPager.getCurrentItem()).onActivityResult(requestCode, resultCode, data);
    }
}